<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="顾客id" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入顾客id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="摄影师id" prop="photographerId">-->
<!--        <el-input-->
<!--          v-model="queryParams.photographerId"-->
<!--          placeholder="请输入摄影师id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="套餐id" prop="comboId">-->
<!--        <el-input-->
<!--          v-model="queryParams.comboId"-->
<!--          placeholder="请输入套餐id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="流程进度" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入流程进度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input
          v-model="queryParams.notes"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="bookingTime">
        <el-date-picker clearable
          v-model="queryParams.bookingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="评价" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入评价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button  v-if="role !== 'photographer'"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['booking:booking:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="role !== 'photographer'"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['booking:booking:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button  v-if="role !== 'photographer'"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['booking:booking:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['booking:booking:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="顾客id" align="center" prop="customerId" />
      <el-table-column label="顾客姓名" align="center" prop="customerName" />
      <el-table-column label="摄影师id" align="center" prop="photographerId" />
      <el-table-column label="摄影师姓名" align="center" prop="photographerName" />
      <el-table-column label="套餐id" align="center" prop="comboId" />
      <el-table-column label="套餐名称" align="center" prop="comboName" />
      <el-table-column label="流程进度" align="center" prop="stateCHN" />
      <el-table-column label="预约地址" align="center" prop="address" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="预约时间" align="center" prop="bookingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评价" align="center" prop="score" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="role !== 'photographer'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['booking:booking:edit']"
          >修改</el-button>
          <el-button  v-if="role !== 'photographer'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['booking:booking:remove']"
          >删除</el-button>
          <el-button v-if="role === 'photographer' && scope.row.state === 1"
                     size="mini"
                     type="text"
                     icon="el-icon-check"
                     @click="handleCheck(scope.row)"
                     v-hasPermi="['booking:booking:edit']"
          >接受</el-button>
          <el-button  v-if="role === 'photographer' && scope.row.state === 1"
                      size="mini"
                      type="text"
                      icon="el-icon-close"
                      @click="handleClose(scope.row)"
                      v-hasPermi="['booking:booking:edit']"
          >拒绝</el-button>
          <el-button v-if="role === 'photographer' && scope.row.state === 2"
                     size="mini"
                     type="text"
                     icon="el-icon-check"
                     @click="handleFinish(scope.row)"
                     v-hasPermi="['booking:booking:edit']"
          >完成</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="顾客id" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入顾客id" />
        </el-form-item>
        <el-form-item label="摄影师id" prop="photographerId">
          <el-input v-model="form.photographerId" placeholder="请输入摄影师id" />
        </el-form-item>
        <el-form-item label="套餐id" prop="comboId">
          <el-input v-model="form.comboId" placeholder="请输入套餐id" />
        </el-form-item>
        <el-form-item label="流程进度" prop="state">
          <el-input v-model="form.state" placeholder="请输入流程进度" />
        </el-form-item>
        <el-form-item label="预约地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入预约地址" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="预约时间" prop="bookingTime">
          <el-date-picker clearable
            v-model="form.bookingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评价" prop="score">
          <el-input v-model="form.score" placeholder="请输入评价" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBooking, getBooking, delBooking, addBooking, updateBooking } from "@/api/booking/booking";
import {getInfo} from "@/api/login";

export default {
  name: "Booking",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约信息表格数据
      bookingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        photographerId: null,
        comboId: null,
        state: null,
        notes: null,
        address: null,
        bookingTime: null,
        score: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      role: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询预约信息列表 */
    getList() {
      this.loading = true;
      getInfo().then(response => {
        this.role = response.roles[0];
        if(this.role !== "admin" && this.role !== "administrator"){
          this.queryParams.photographerId = response.user.userId;
        }
        listBooking(this.queryParams).then(response => {
          this.bookingList = response.rows;
          //循环遍历，将state转换为中文
          for(let i = 0; i < this.bookingList.length; i++){
            if(this.bookingList[i].state === 1){
              this.bookingList[i].stateCHN = "待接受";
            }else if(this.bookingList[i].state === 2) {
              this.bookingList[i].stateCHN = "已接受";
            }else if(this.bookingList[i].state === 3) {
              this.bookingList[i].stateCHN = "已完成";
            }else if(this.bookingList[i].state === 4) {
              this.bookingList[i].stateCHN = "已拒绝";
            }else {
              this.bookingList[i].stateCHN = "状态未知";
            }
          }
          this.total = response.total;
          this.loading = false;
        });
      });


    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        photographerId: null,
        comboId: null,
        state: null,
        notes: null,
        address: null,
        bookingTime: null,
        score: null,
        createTime: null,
        updateTime: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBooking(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约信息";
      });
    },
    /** 接受预约按钮操作 */
    handleCheck(row) {
      console.log(row.id);
      getBooking(row.id).then(response => {
        this.form = response.data;
        this.form.state = 2;
        updateBooking(this.form).then(response => {
          this.$modal.msgSuccess("预约成功");
          this.getList();
        });
      });
    },
    /** 拒绝预约按钮操作 */
    handleClose(row) {
      console.log(row.id);
      getBooking(row.id).then(response => {
        this.form = response.data;
        this.form.state = 4;
        updateBooking(this.form).then(response => {
          this.$modal.msgSuccess("拒绝成功");
          this.getList();
        });
      });
    },
    /** 完成预约按钮操作 */
    handleFinish(row) {
      console.log(row.id);
      getBooking(row.id).then(response => {
        this.form = response.data;
        this.form.state = 3;
        updateBooking(this.form).then(response => {
          this.$modal.msgSuccess("完成！");
          this.getList();
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBooking(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBooking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预约信息编号为"' + ids + '"的数据项？').then(function() {
        return delBooking(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('booking/booking/export', {
        ...this.queryParams
      }, `booking_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
